import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
	static WeightedDirectedGraph<String, Integer> graph = new WeightedDirectedGraph<String, Integer>();
	final static int AANTAL_ELEMENTEN = 9;
	final static String VERTICES_BESTANDSNAAM = "Vertices.txt";
	final static String NODES_BESTANDSNAAM = "Nodes.txt";

	public static void main(String[] args) {
		makeVerticesAndWrite(VERTICES_BESTANDSNAAM);
		readFileAndPut(VERTICES_BESTANDSNAAM);
		readFileAndlinkVertices(NODES_BESTANDSNAAM);
		//TODO vroegste en laatste invullen
	}

	public static void readFileAndPut(String fileName) {
		try {
			BufferedReader file = new BufferedReader(new FileReader(fileName));
			;
			while (file.ready()) {
				graph.addItem(file.readLine());
			}
			System.out
					.println("Done adding elements to the graph. Not linked yet.");
			System.out.println("Size of the graph so far: " + graph.size());
			System.out.println("---");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void readFileAndlinkVertices(String fileName) {
		try {
			BufferedReader file = new BufferedReader(new FileReader(fileName));
			;
			while (file.ready()) {
				String[] gegevens = file.readLine().split(" = ");
				int cost = Integer.parseInt(gegevens[1]);
				String[] verticesToLink = gegevens[0].split(" -> ");
				graph.addEdge(verticesToLink[0], verticesToLink[1], cost);
			}
			System.out.println("Linking succeeded.");
			System.out.println("---");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void makeVerticesAndWrite(String fileName) {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
			for (int i = 0; i < AANTAL_ELEMENTEN; i++) {
				writer.write(maakNaam(i));
				writer.newLine();
			}
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static String maakNaam(int state) {
		char[] charArray = new char[2];
		charArray[0] = (char) (state / 26 + 'A');
		charArray[1] = (char) (state % 26 + 'A');

		return new String(charArray);
	}
}
